package gate_event

import (
	"base"
	"errors"
	"fmt"
)

type (
	onRegisterEvent struct {
	}
	onRegisterCallback = func(agentId base.AgentId, innerAddr string)
)

var OnRegister = new(onRegisterEvent)

func (event *onRegisterEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(onRegisterCallback); !ok {
		err = errors.New("not on register event callback function")
	}

	return
}

func (event *onRegisterEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 2
	if len(args) < ArgsCount {
		err = fmt.Errorf("on register event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(onRegisterCallback)
	i := 0
	agentId := args[i].(base.AgentId)
	i++
	innerAddr := args[i].(string)
	i++

	call(agentId, innerAddr)

	return
}

type (
	onCloseEvent struct {
	}
	onCloseCallback = func(agentId base.AgentId, innerAddr string)
)

var OnClose = new(onCloseEvent)

func (event *onCloseEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(onCloseCallback); !ok {
		err = errors.New("not on close event callback function")
	}

	return
}

func (event *onCloseEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 1
	if len(args) < ArgsCount {
		err = fmt.Errorf("on close event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(onCloseCallback)
	i := 0
	agentId := args[i].(base.AgentId)
	i++
	innerAddr := args[i].(string)
	i++

	call(agentId, innerAddr)

	return
}
